package com.example.cp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cp.config.HiddenProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

/**
 * Created by hemin on 2019/8/16.
 */
@Controller
@RequestMapping("/hidden")
@Slf4j
public class HiddenController {

    private String prefix = "system/hidden";

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    HiddenProperties hiddenProperties;

    //TODO 模拟网页授权登陆
    @GetMapping("/index")
    public String index(ModelMap modelMap){
        String url = hiddenProperties.getHeaderUrl() + "/userInfo/isBind?userId=a5a5ccc7be597da47dfd6362a03859c2";
        JSONObject result = restTemplate.getForObject(url, JSONObject.class);
        modelMap.put("msg",result.getString("statusMsg"));
        modelMap.put("data",result.getJSONObject("data").toJSONString());
        modelMap.put("userData",result.getJSONObject("data"));
        //如果已绑定则跳转到隐患首页
        String id = (String) result.getJSONObject("data").get("id");
        //获取隐患通报数据
        JSONObject forObject = restTemplate.getForObject(hiddenProperties.getHeaderUrl() + "/danger/report?userId=" + id, JSONObject.class);
        modelMap.put("report",forObject);
        //获取隐患首页数据 1.隐患首页数据 2.隐患通报
        String latestUrl = hiddenProperties.getHeaderUrl() + "/danger/latest?userId="+id+"count=6";
        JSONArray latests = restTemplate.getForObject(latestUrl, JSONArray.class);
        modelMap.put("latests",latests);
        return prefix + "/index_";
    }

    //TODO 模拟未绑定状态
    @GetMapping("/bind")
    public String bind(ModelMap modelMap){
        modelMap.put("userId","HeMinMin");
        return "bind";
    }

    /**
     * 跳转主页
     */
    @GetMapping("/index_")
    public String index(@RequestParam String openId,ModelMap modelMap) {
        String url = hiddenProperties.getHeaderUrl() + "userInfo/isBind?userId="+openId;
        JSONObject result = restTemplate.getForObject(url, JSONObject.class);
        //拼接数据
        modelMap.put("msg",result.getString("statusMsg"));
        modelMap.put("data",result.getJSONObject("data").toJSONString());
        modelMap.put("userData",result.getJSONObject("data"));

//        modelMap.put("openId",openId);
        return prefix + "/index_";
    }

    /**
     * 跳转隐患首页
     */
    @GetMapping("/home")
    public String hidden(@RequestParam String openId,ModelMap modelMap) {
        String url = hiddenProperties.getHeaderUrl() + "userInfo/isBind?userId="+openId;
        JSONObject result = restTemplate.getForObject(url, JSONObject.class);
        //拼接数据
        modelMap.put("msg",result.getString("statusMsg"));
        modelMap.put("data",result.getJSONObject("data").toJSONString());
        modelMap.put("userData",result.getJSONObject("data"));
        //如果已绑定则跳转到隐患首页
        String id = (String) result.getJSONObject("data").get("id");
        //获取隐患通报数据
        JSONObject forObject = restTemplate.getForObject(hiddenProperties.getHeaderUrl() + "/danger/report?userId=" + id, JSONObject.class);
        modelMap.put("report",forObject);
        //获取隐患首页数据 1.隐患首页数据 2.隐患通报
        String latestUrl = hiddenProperties.getHeaderUrl() + "/danger/latest?userId="+id+"&count=6";
        JSONArray latests = restTemplate.getForObject(latestUrl, JSONArray.class);
        modelMap.put("latests",latests);
        return prefix + "/home";
    }

    /**
     * 跳转到部门隐患筛选页面
     */
    @GetMapping("/searchDept")
    public String searchDept() {
        return prefix + "/hidden-search-dept";
    }

    /**
     * 跳转隐患筛选页面
     */
    @GetMapping("/search")
    public String search() {
        return prefix + "/hidden-search";
    }

    /**
     * 跳转隐患首页
     */
    @GetMapping("/form")
    public String form() {
        return prefix + "/form";
    }

    /**
     *
     * @return
     */
    @GetMapping("/fill/{isLeader}")
    public String fill(@PathVariable(value = "isLeader")boolean isLeader, ModelMap map){
        map.put("isLeader",isLeader);
        return prefix + "/hidden-fill";
    }
    /**
     *
     * @return
     */
    @GetMapping("/fill-limit/{isLeader}")
    public String limit(@PathVariable(value = "isLeader")boolean isLeader, ModelMap map){
        map.put("isLeader",isLeader);
        return prefix + "/hidden-fill-deadline";
    }

    /**
     * 跳转我的隐患列表页面
     * @param userId 用户id
     * @param param 其他参数，筛选时用
     * @param map 回调参数
     * @return 跳转隐患列表页面，传递隐患数据
     */
    @GetMapping("/list")
    public String list(@RequestParam("userId")String userId,
                         @RequestParam(value = "param",required = false)String param,
                         ModelMap map) {
        System.out.println(param);
        String url = hiddenProperties.getHeaderUrl()
                + "/danger/list?dataStatus={dataStatus}&userId={userId}&"+param;
        JSONArray jsonArray1 = restTemplate.getForObject(url, JSONArray.class,"1",userId);
        JSONArray jsonArray2 = restTemplate.getForObject(url, JSONArray.class,"2",userId);
        JSONArray jsonArray3 = restTemplate.getForObject(url, JSONArray.class,"3",userId);
        map.put("data1",jsonArray1);//待处理
        map.put("data2",jsonArray2);//已处理
        map.put("data3",jsonArray3);//已填报
        return prefix + "/hidden-list";
    }

    /**
     * 跳转本单位隐患列表页面
     * @param userId 用户id
     * @param param 其他参数，筛选时用
     * @param map 回调参数
     * @return 跳转隐患列表页面，传递隐患数据
     */
    @GetMapping("/deptList")
    public String deptList(@RequestParam("userId")String userId,
                         @RequestParam(value = "param",required = false)String param,
                         ModelMap map) {
        log.info("deptId:{deptId},param:{param}",userId,param);
        JSONArray data1 = restTemplate.getForObject(hiddenProperties.getHeaderUrl() + "/danger/list?dataStatus=4&userId="+userId+"&"+param, JSONArray.class);
        JSONArray data2 = restTemplate.getForObject(hiddenProperties.getHeaderUrl() + "/danger/list?dataStatus=5&userId="+userId+"&"+param, JSONArray.class);
        map.put("data1",data1);
        map.put("data2",data2);
        return prefix + "/hidden-list-dept";
    }

    /**
     * 跳转详情页，若果是未提交状态，则跳转到隐患填报页面
     * @param entityId 隐患id
     * @param status 隐患状态
     * @param index 索引 0待整改 1已处理 2已填报
     * @param map 传输数据
     * @return 页面地址
     */
    @GetMapping("/details")
    public String details(@RequestParam("userId")String entityId,
                          @RequestParam("status") String status,
                          @RequestParam(value = "rectifyStatus",defaultValue = "0",required = false) Integer rectifyStatus,
                          @RequestParam("index") Integer index,
                          @RequestParam("isLeader") boolean isLeader,
                          ModelMap map) {
        String detailsUrl = hiddenProperties.getHeaderUrl() + "/danger/danger?id=" + entityId;
        String logsUrl = hiddenProperties.getHeaderUrl() + "/log/list?entityId=" + entityId;
        JSONObject detailsObject = restTemplate.getForObject(detailsUrl, JSONObject.class);
        JSONArray logsObject = restTemplate.getForObject(logsUrl, JSONArray.class);

        map.put("details",detailsObject.get("data"));
        map.put("logs",logsObject);
        map.put("status",status);
        map.put("rectifyStatus",rectifyStatus);
        map.put("index",index);
        map.put("isLeader",isLeader);

        if(status!=null && (status.equals("0") || status.equals("5"))){
            return prefix + "/hidden-fill-edit";
        }
        return prefix + "/hidden-details";
    }

    /**
     * 跳转详情页，若果是未提交状态，则跳转到隐患填报页面
     * @param entityId 隐患id
     * @param map 传输数据
     * @return 页面地址
     */
    @GetMapping("/detailsDept")
    public String detailsDept(@RequestParam("entityId")String entityId,
                          ModelMap map) {
        String detailsUrl = hiddenProperties.getHeaderUrl() + "/danger/danger?id=" + entityId;
        String logsUrl = hiddenProperties.getHeaderUrl() + "/log/list?entityId=" + entityId;
        JSONObject detailsObject = restTemplate.getForObject(detailsUrl, JSONObject.class);
        JSONArray logsObject = restTemplate.getForObject(logsUrl, JSONArray.class);

        map.put("details",detailsObject.get("data"));
        map.put("logs",logsObject);
        return prefix + "/hidden-details-dept";
    }

    @GetMapping("/report")
    public String report(@RequestParam String userId,ModelMap map){
        String url = hiddenProperties.getHeaderUrl() + "/danger/latest?userId={userId}";
        JSONArray data = restTemplate.getForObject(url, JSONArray.class, userId);
        map.put("data",data);
        return prefix + "/home-bulletin";
    }

    @GetMapping("/library")
    public String library(@RequestParam(value = "params",required = false) String params,
                          ModelMap map){
        String url = hiddenProperties.getHeaderUrl() + "/danger/bankList";
        if(StringUtils.isNotEmpty(params)){
            url = hiddenProperties.getHeaderUrl() + "/danger/bankList?" + params;
        }
        JSONArray data = restTemplate.getForObject(url, JSONArray.class);
        map.put("data",data);
        return prefix + "/library";
    }

    @GetMapping("chart")
    public String chart(@RequestParam(value = "userId") String userId,
                          ModelMap map){
        String url = hiddenProperties.getHeaderUrl() + "/danger/dangerTj?userId={userId}";
        JSONObject data = restTemplate.getForObject(url, JSONObject.class,userId);
        map.put("data",data);
        return prefix + "/chart";
    }


    @GetMapping("/echart")
    public String echart(@RequestParam String dataType,ModelMap map){

        String url = hiddenProperties.getHeaderUrl() + "/echart/gethidePies?dataType={dataType}";
        JSONArray forObject = restTemplate.getForObject(url, JSONArray.class, dataType);
        map.put("data",forObject);

        if (dataType=="0"){
            return prefix+"/hidden-echart-type";
        }else {
            return prefix+"/hidden-echart-Grade";
        }
    }


    @GetMapping("/echart_day")
    public String echart_day(@RequestParam String dataType,ModelMap map){

        String url = hiddenProperties.getHeaderUrl() + "/echart/gethideLines?dataType={dataType}";
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class, dataType);
        map.put("data",forObject);

        if (dataType=="0"){
            return prefix+"/hidden-echart-day";
        }else {
            return prefix+"/hidden-echart-month";
        }
    }

}
